package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.*;
import com.situ.mall.common.mapper.*;
import com.situ.mall.common.service.IOrderInfoService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单主表 服务实现类
 * </p>
 *
 * @author 闫佳宁
 * @since 2023-09-22
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private MemberAddressMapper memberAddressMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)//事务
    public OrderInfo saveOrder(String ids, Integer addressId) {

        Member m = (Member) RequestUtil.getCurrent();


        //创建一个OrderInfo对象 保存进数据库
        OrderInfo oi = new OrderInfo();
        //订单号 用逻辑写
        oi.setCode("O-" + System.currentTimeMillis());
        //创建时间
        oi.setCreateTime(LocalDateTime.now());
        //支付状态
        oi.setStatus("待支付");
        //会员ID
        oi.setMemberId(m.getId());
        //付款
        oi.setPayType("alipay");
        //总价
        BigDecimal sum = new BigDecimal("0");
        //算总价格
        for (String cid : ids.split(",")) {
            //把cid转成数给到cartMapper把购物车当前那条对象取出来
            Cart c = cartMapper.selectById(Integer.parseInt(cid));
            //再让ProductMapper帮我查一下这个商品的商品信息
            Product p = productMapper.selectById(c.getProductId());
            //数量没用BigDecimal需要转一下
            //需要重新给sum赋值
            sum = sum.add(p.getPrice().multiply(new BigDecimal(c.getNumber())));
        }
        //订单总价格
        oi.setPrice(sum);
        baseMapper.insert(oi);

        //创建多个OrderDetail 保存进数据库
        for (String cid : ids.split(",")) {
            //把cid转成数给到cartMapper把购物车当前那条对象取出来
            Cart c = cartMapper.selectById(Integer.parseInt(cid));
            //再让ProductMapper帮我查一下这个商品的商品信息
            Product p = productMapper.selectById(c.getProductId());
            //每遍历一个商品创建一个OrderDetail
            OrderDetail od = new OrderDetail();
            //订单ID      plus
            od.setOrderId(oi.getId());
            //数量
            od.setAmount(c.getNumber());
            //老价格
            od.setOldPrice(p.getPrice());
            //商品ID
            od.setProductId(c.getProductId());

            orderDetailMapper.insert(od);
        }

        //创建OrderAddress对象 保存进数据库
        MemberAddress ma = memberAddressMapper.selectById(addressId);

        OrderAddress oa = new OrderAddress();

        //拷贝属性 源 目标
        BeanUtils.copyProperties(ma,oa);//用时注意导包问题
        oa.setId(null);
        oa.setOrderId(oi.getId());
        orderAddressMapper.insert(oa);

        //把购物车中的原来的数据删除
        for(String cid : ids.split(",")){
            cartMapper.deleteById(Integer.parseInt(cid));
        }
        return oi;
    }

    @Override
    public TableVO selectOrder(Integer page, Integer limit, String username, String status1) {
        //pageHelper 分页小工具
        PageHelper.startPage(page,limit);
        //上面这行代码后跟的第一次查数据库是分页查询，只生效一次
        return  TableVO.data(orderInfoMapper.selectOrder(username,status1));
    }

    @Override
    public TableVO orderDetail(Integer id) {
        return TableVO.data(orderInfoMapper.orderDetail(id));
    }

    @Override
    public void update(Integer id) {
        orderInfoMapper.update1(id);
    }

    @Override
    public List<OrderInfo> selectOrderInfo(Integer id) {
        return orderInfoMapper.selectOrderInfo(id);
    }

    @Override
    public void paySuccess(String code) {
        baseMapper.paySuccess(code);
    }

    @Override
    public void confirm(Integer id) {
        orderInfoMapper.confirm(id);
    }


}
